home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_08 / 9n08075a < prev    next >
Text File  |  1991-06-20  |  2KB  |  79 lines

  1. //  Test program for COW objects
  2.  
  3. #include <stdio.h>
  4. #include "cow.h"
  5.  
  6. class Foo_COW : Obj_COW {
  7.     friend class Foo;
  8. public:
  9.     //  Construtors & destructors
  10.     Foo_COW(int l, int m, int n) { a = l; b = m; c = n; }
  11.     //  Functions
  12.     Obj_COW *dup(void)
  13.       { return((Obj_COW *)new Foo_COW(a, b, c)); }
  14. private:
  15.     //  Data
  16.     int a, b, c;
  17. };
  18.  
  19. class Foo : Obj_Virt {
  20. public:
  21.     //  Construtors & destructors
  22.     Foo(int i = 1, int j = 2, int k = 3)
  23.       { set_ptr(new Foo_COW(i, j, k)); }
  24.     Foo(const Foo& f2) : Obj_Virt((Obj_Virt &)f2) { }
  25.     //  Functions
  26.     Foo&    operator = (Foo& f2)
  27.               { set_ptr(f2.get_ptr()); return(*this); }
  28.     int     get_a(void) { return(actual()->a); }
  29.     int     get_b(void) { return(actual()->b); }
  30.     int     get_c(void) { return(actual()->c); }
  31.     void    print(char *msg);
  32.     void    reset(int i = 1, int j = 2, int k = 3);
  33. private:
  34.     //  Functions
  35.     Foo_COW *actual(void)
  36.       { return((Foo_COW *)get_ptr()); }
  37. };
  38.  
  39. //  Foo::print - print current values
  40. void Foo::print(char *msg)
  41. {
  42.     printf("%-12s: this = %p, ", msg, this);
  43.     Obj_Virt::print();
  44.     printf("    a, b, c = %d %d %d\n", actual()->a,
  45.       actual()->b, actual()->c);
  46. }
  47.  
  48. //  Foo::reset - reset values in real object
  49. void Foo::reset(int i, int j, int k)
  50. {
  51.     split();
  52.     actual()->a = i;
  53.     actual()->b = j;
  54.     actual()->c = k;
  55. }
  56.  
  57. main()
  58. {
  59.     Foo *pa1 = new Foo;
  60.     Foo *pa2 = new Foo(*pa1);
  61.     Foo *pa3 = new Foo(4, 5, 6);
  62.  
  63.     pa1->print("a1");
  64.     pa2->print("a2 (= a1?)");
  65.     pa3->print("a3");
  66.  
  67.     *pa3 = *pa2;
  68.     pa3->print("a3 (= a2?)");
  69.     pa2->print("a2 (same?)");
  70.  
  71.     pa3->reset(7, 8, 9);
  72.     pa3->print("a3 reset");
  73.     pa2->print("a2 (same?)");
  74.  
  75.     delete pa1;
  76.     delete pa2;
  77.     delete pa3;
  78. }
  79.